var cityData3 = [];
var townData = [];
function cityInit() {
	var cityPicker3 = [];

	/*第一级组装*/
	var provinceId,cityId;
	$.ajax({
		type:"get",
		url:basePathUrl+"api/district/by/param?param_level=1",
		async:false,
		success : function(a){
			var province = eval("("+a.data+")");//后台获取
			addProvince(province);
		}
	});
	cityPicker3 = new mui.PopPicker({
		layer: 3
	});
	var provinceCode = getProvinceById(document.getElementById('provinceCode').value);
	if(provinceCode != null){
		cityPicker3.pickers[0].list.angle = provinceCode.coord * 20;
		getArea(provinceCode);
		var cityCode = getCityById(document.getElementById('cityCode').value);
		if(cityCode != null){
			cityPicker3.pickers[1].list.angle = cityCode.coord * 20;
			getArea(cityCode);
			var areaCode = getAreaById(document.getElementById('areaCode').value,cityCode);
			if(areaCode != null){
				cityPicker3.pickers[2].list.angle = areaCode.coord * 20;
			}
		}
	}
	return cityPicker3;
}
function townInit(){
	var area;
	var city = getCityById(document.getElementById('cityCode').value);
	if(city != null){
		area = getAreaById(document.getElementById('areaCode').value,city);
	}
	var townPicker = [];
	townPicker = new mui.PopPicker({
		layer: 1
	});
	if(area != null){
		setTown(townPicker);
		var townCode = getTownById(document.getElementById('townCode').value,area);
		if(townCode != null){
			townPicker.pickers[0].list.angle = townCode.coord * 20;
		}
	}
	return townPicker;
}
function setTown(townPicker){
	var area;
	var city = getCityById(document.getElementById('cityCode').value);
	if(city != null){
		 area = getAreaById(document.getElementById('areaCode').value,city);
	}
	if(area==null){
		mui.toast("请选择所属区域!");
		return false;
	}
	if(area.children.length == 0 && area.level == '3'){
		townData = [];
		/*第四级组装*/
		$.ajax({
			type:"get",
			url:basePathUrl+"api/district/by/param?param_upid="+area.value,
			async:false,
			success : function(a){
				if(a.data=='[]'){
					if(!isInitializeTown){
						mui.toast("该区域没有街道信息!");
					}
					isInitializeTown = false;
					return false;
				}
				var town = eval("("+a.data+")");//后台获取
				addTown(town,area);
			}
		});
	}
	else{
		townData = area.children;
	}
	townPicker.setData(townData);
	return true;
}
function addProvince(e){
	for(var i = 0; i < e.length; i++){
		var a = {};
		a.text = e[i].name;
		a.value = e[i].id;
		a.upid = e[i].upid;
		a.children = [];
		a.level = e[i].level;
		a.coord = i;
		cityData3.push(a);
	}
}
function addCity(e,parent) {
	for(var i = 0; i < e.length; i++){
		var a = {};
		var province = parent;
		a.text = e[i].name;
		a.value = e[i].id;
		a.upid = e[i].upid;
		a.children = [];
		a.level = e[i].level;
		a.coord = i;
		a.parent = province;
		province.children.push(a);
	}
}
function addArea(e,parent){
	for(var i = 0; i < e.length; i++){
		var a = {};
		var city = parent;
		a.text = e[i].name;
		a.value = e[i].id;
		a.upid = e[i].upid;
		a.children = [];
		a.level = e[i].level;
		a.parent = city;
		a.coord = i;
		city.children.push(a);
	}
}
function addTown(e,parent){
	for(var i = 0; i < e.length; i++){
		var a = {};
		var area = parent;
		a.text = e[i].name;
		a.value = e[i].id;
		a.upid = e[i].upid;
		a.level = e[i].level;
		a.parent = area;
		a.coord = i;
		area.children.push(a);
		townData.push(a);
	}
}
function getProvinceById(id){
	for(var i = 0; i < cityData3.length; i++){
		var province = cityData3[i];
		if(province.value == id){
			return province;
		}
	}
	return null;
}
function getCityById(id){
	for(var i = 0; i < cityData3.length; i++){
		var province = cityData3[i];
		for(var j = 0; j < province.children.length; j++){
			var city = province.children[j];
			if(city.value == id){
				return city;
			}
		}
	}
	return null;
}
function getAreaById(id,city){
	for(var i = 0; i < city.children.length; i++){
		var area = city.children[i];
		if(area.value == id){
			return area;
		}
	}
	return null;
}

function getTownById(id,area){
	for(var i = 0; i < area.children.length; i++){
		var town = area.children[i];
		if(town.value == id){
			return town;
		}
	}
	return null;
}

function getArea(preItem){
	/*获取城市 再得到地区*/
	if(preItem.parent == null && preItem.children != null && preItem.children.length == 0 && preItem.level == '1'){
		var cityId,city_1;
		$.ajax({
			type:"get",
			url:basePathUrl+"api/district/by/param?param_upid="+preItem.value,
			async:false,
			success : function(a){
				if(a.data != '' && a.data != null){
					var city = eval("("+a.data+")");//后台获取
					addCity(city,preItem);
					city_1 = city[0];
				}
			}
		});
		//if(city_1 != null){
		//	$.ajax({
		//		type:"get",
		//		url:basePathUrl+"api/district/by/param?param_upid="+city_1.value,
		//		async:false,
		//		success : function(a){
		//			var area = eval("("+a.data+")");//后台获取
		//			addArea(area,city_1);
		//		}
		//	});
		//}
	}
	/*已有城市 得到地区*/
	else if(preItem.children != null && preItem.children.length == 0 && preItem.level == '2'){
		$.ajax({
			type:"get",
			url:basePathUrl+"api/district/by/param?param_upid="+preItem.value,
			async:false,
			success : function(a){
				var area = eval("("+a.data+")");//后台获取
				addArea(area,preItem);
			}
		});
	}
}






















